#include "includes.h"
#include "Screen.h"
#include "PrototipoFisico.h"


static unsigned long lastTime=0;

void updateWindows(unsigned long elapsedTime);


static unsigned long timePassed(){
	unsigned long timePassed= SDL_GetTicks() - lastTime;
	lastTime=SDL_GetTicks();
	return timePassed;
}


void setup(){
	
	Screen::instance()->init(800,600,false);

}


int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd){

	char caption[250];
	unsigned long timePass=0;
	unsigned long timeSinceLastSecond=0;
	int frames=0;
	int fps=0;


	setup();

	SDL_WM_SetCaption (":)",NULL);

	while(true){

		timePass=(unsigned long)timePassed();
		timeSinceLastSecond+=timePass;
		frames++;

		if(timeSinceLastSecond>=1000){
			timeSinceLastSecond=timeSinceLastSecond-1000;
			fps=frames;
			frames=0;			
			SDL_WM_SetCaption (caption, NULL);
		}

		updateWindows(timePass);
		PrototipoFisico::instance()->update(timePass);
		Screen::instance()->draw();
		
	}

	return 0;
}



void updateWindows(unsigned long elapsedTime){
	SDL_Event evnt;

	// Check for evnts
	while (SDL_PollEvent (&evnt)){
		switch (evnt.type){
			case SDL_QUIT:
				exit(0);
			default:
				break;
		}
	}
	if ( evnt.key.keysym.sym == SDLK_ESCAPE ) {
		//exit(0);
	}
	SDL_WM_SetCaption (":)",NULL);
}
